package ua.kpi_java_training.evgeniygusar.module3;

import java.util.ArrayList;

public class AllOstriches {

    int OSTRICH_BIRTH_CYCLE = 3;

    int index_of_nearest_ostrich = 0;
    int X_BORDER, Y_BORDER;
    Desert mydesert = new Desert();

    public AllOstriches() {

        X_BORDER = mydesert.getXBorder();
        Y_BORDER = mydesert.getYBorder();
    }

    int x_of_nearest_ostrich = X_BORDER+1, y_of_nearest_ostrich = Y_BORDER+1;

    static private ArrayList<Ostrich> ostriches = new ArrayList<Ostrich>();

    /**
     * Getter for OSTRICH_BIRTH_CYCLE
     */

    public int getOstrichBirthCycle() {
        return OSTRICH_BIRTH_CYCLE;
    }

    /**
     * Method for adding new ostrich
     */

    public void addOstrich() {
        ostriches.add(new Ostrich());
    }

    /**
     * Getter for ostrich object with certain index
     * @param position - index of ostrich
     * @return ostrich object
     */

    public Ostrich getOstrich(int position) {
        return ostriches.get(position);
    }

    /**
     * Destructor for ostrich with certain index
     */

    public void removeOstrich(int position) {
        ostriches.remove(position);
    }

    /**
     * Method for moving of all ostriches
     */

    public void ostrichesMoving() {
        for(Ostrich o: ostriches) {
            o.Moving();
        }
    }

    /**
     * Output of new ostriches coordinats
     */

    public void displayOstrichesMoving() {
        for(Ostrich o: ostriches) {
            System.out.print("Ostrich " + (ostriches.indexOf(o) + 1) + ": x = " + o.x);
            System.out.println(", y = " + o.y);
        }
    }

    /**
     * Getter for numbers of all ostriches
     */

    public int numbersOfOstriches() {
        return ostriches.size();
    }

    /**
     * Method for finding nearest ostrich to leopard
     * @param x - x coordinat of leopard
     * @param y - y coordinat of leopard
     */

    public void nearestOstrich(int x, int y) {
        double shortest_distance, current_distance;

        shortest_distance = (X_BORDER+1)*(X_BORDER+1) + (Y_BORDER+1)*(Y_BORDER+1);
        for(Ostrich o: ostriches) {
            current_distance = Distance(x-o.x, y-o.y);
            if(current_distance < shortest_distance) {
                shortest_distance = current_distance;
                index_of_nearest_ostrich = ostriches.indexOf(o);
                x_of_nearest_ostrich = o.x;
                y_of_nearest_ostrich = o.y;
            }
        }
    }

    /**
     * Method for calculation of hypotenuse via Pifagor theorem
     * @param x - length of first cathetus of triangle
     * @param y - length of second cathetus of triangle
     * @return  - length of hypotenuse of triangle in Pifagor theorem
     */

    public double Distance(int x, int y) {
        return Math.sqrt(x*x + y*y);
    }

    /**
     * Getter for x_of_nearest_ostrich
     */

    public int getXOfNearestOstrich(){
        return x_of_nearest_ostrich;
    }

    /**
     * Getter for y_of_nearest_ostrich
     */

    public int getYOfNearestOstrich(){
        return y_of_nearest_ostrich;
    }

    /**
     * Getter for index_of_nearest_ostrich
     */

    public int getIndexOfNearestOstrich(){
        return index_of_nearest_ostrich;
    }
}
